package book;

import data.BookData;
import data.UserData;
import Date.Date_s;

import java.util.Scanner;

public class Book {
    public static BookData book=new BookData();
    UserData user=new UserData();
    //管理员登录验证
    public int login(String username,String password){
        if(username.equals(user.getUsername())){
            if(password.equals(user.getPassword())){
                return 1;
            }else{
                return 0;
            }
        }else{
            return -1;
        }
    }
    /*菜单功能展示列表
     *
     * 图书信息展示功能
     */
    public void showBook(){
        System.out.println("本图书馆共有"+(book.booknames.length-remainSpace())+"本书");
        int a=bookinfo();
        if(a==0){
            //展示所有图书信息
            showAll();
        }else if(a>0 && a<=book.booknames.length){
            //展示部分图书信息
            showPart(a);
        }else{
            System.out.println("输入的编号不合法!");
            showBook();
        }

    }

    public void showAll(){
        for(int i=0;i<book.booknames.length;i++){
            if(book.booknames[i]!=null){
               //System.out.println("--------------编号"+(i+1)+"------------------");
                System.out.println("----------------------------------");
               // System.out.println("书籍编号为"+(i+1)+"的详细信息为:");
                System.out.print("书名:"+book.booknames[i]+" ");
                System.out.print("作者:"+book.authors[i]+" ");
                System.out.print("出版社:"+book.presss[i]+" ");
                System.out.print("图书类型:"+book.types[i]+" ");
                System.out.println("借阅者:"+book.borrowreaders[i]);
            }
        }
        System.out.println("--------------------------------------------------------------");
    }

    public void showPart(int a){
        int i=a;
        System.out.println("---------------------------------------------------------------");
        //System.out.println("书籍编号为"+(i)+"的详细信息为:");
        System.out.print("书名:"+book.booknames[i-1]+" ");
        System.out.print("作者:"+book.authors[i-1]+" ");
        System.out.print("出版社:"+book.presss[i-1]+" ");
        System.out.print("图书类型:"+book.types[i-1]+" ");
        System.out.println("借阅者:"+book.borrowreaders[i-1]);
        System.out.println("--------------------------------------------------------------");
    }


    public int bookinfo(){
        for(int i=0;i<book.booknames.length;i++){
            if(book.booknames[i]!=null){
                System.out.println("书籍编号"+(i+1)+":"+book.booknames[i]+" ");
            }
        }
        System.out.print("输入相应编号即可查看相应书籍的详细信息(输入0则可查看所有信息)");
        Scanner scanner=new Scanner(System.in);
        int i=scanner.nextInt();
        return i;
    }

    public int remainSpace(){
        int count=0;
        try{
            for(int i=0;i<book.booknames.length;i++){
                if(book.booknames[i]==null){
                    ++count;
                }
            }

        }catch(Exception e){

        }
        return count;
    }
    public int addBook(String bookname,String authors,String presss,String types){
        int flag=0;//1成功，0存在此书，-1满了
        if(book.booknames[book.booknames.length-1]!=null){
            flag=-1;
        }
        if(book.booknames[book.booknames.length-1]==null){
            for(int i=0;i<book.booknames.length;i++){
                if(bookname.equals(book.booknames[i])){
                    flag=0;
                    break;
                }
                if(!(bookname.equals(book.booknames[i]))){
                    add(bookname,authors,presss,types);
                    flag=1;
                    break;
                }
            }
        }
        return flag;
    }
    //录入新书
    public void add(String bookname, String authors, String presss, String types){
        for(int i=0;i<book.booknames.length;i++){
            if(book.booknames[i]==null){
                book.booknames[i]=bookname;
                book.authors[i]=authors;
                book.presss[i]=presss;
                book.types[i]=types;
                break;
            }
        }

    }



}
